package com.lmiot.xyclick.Util;

import com.lmiot.xyclick.App.MyApp;
import com.lmiot.xyclick.Bean.ChangeAutoBean;
import com.lkx.liandianqinew.R;
import com.lmiot.xyclick.Util.LoopUtils;
import com.lmiot.xyclick.inteface.OnBasicListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import org.greenrobot.eventbus.EventBus;

public class RawUtilsNew {
    public static void getAllRawFile() {
        Field[] declaredFields = R.raw.class.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            try {
                String name = declaredFields[i].getName();
                int i2 = declaredFields[i].getInt(R.raw.class);
                if (name.startsWith("demo")) {
                    File file = new File(MyApp.getContext().getFilesDir(), "back.xyf");
                    if (!file.exists()) {
                        new File(file.getParent()).mkdirs();
                        file.createNewFile();
                    }
                    saveToFile(i2, file.getAbsolutePath());
                    LoopUtils.unZipBackFile(file, new LoopUtils.onUnZipFinishListener() {
                        /* class com.lmiot.xyclick.Util.RawUtilsNew.AnonymousClass1 */

                        @Override // com.lmiot.xyclick.Util.LoopUtils.onUnZipFinishListener
                        public void result(final boolean z) {
                            FileUtils.resetLocalData(new OnBasicListener() {
                                /* class com.lmiot.xyclick.Util.RawUtilsNew.AnonymousClass1.AnonymousClass1 */

                                @Override // com.lmiot.xyclick.inteface.OnBasicListener
                                public void result(boolean z, String str) {
                                    if (z) {
                                        EventBus.getDefault().post(new ChangeAutoBean(""));
                                    }
                                }
                            });
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void saveToFile(int i, String str) {
        try {
            InputStream openRawResource = MyApp.getContext().getResources().openRawResource(i);
            byte[] bArr = new byte[1024];
            FileOutputStream fileOutputStream = new FileOutputStream(str);
            while (true) {
                int read = openRawResource.read(bArr);
                if (read != -1) {
                    fileOutputStream.write(bArr, 0, read);
                    fileOutputStream.flush();
                } else {
                    openRawResource.close();
                    fileOutputStream.close();
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
